from __future__ import annotations

from typing import Any, Optional

from backend.logger_setup import get_logger

from .repositories import message_repository, session_repository
from .usecases import ChatService

logger = get_logger("chat.manager")


class ChatManager:
    """Facade that preserves the previous ChatManager API surface."""

    def __init__(self, service: Optional[ChatService] = None):
        # Default to shared repository instances to keep DB access consistent.
        self.service = service or ChatService(message_repository, session_repository)

    def process_message(
        self,
        session_id: str,
        message: str,
        use_rag: bool = True,
        use_tools: bool = True,
        stream: Optional[bool] = None,
    ) -> Any:
        return self.service.process_message(session_id, message, use_rag, use_tools, stream)

    def handle_tool_call(self, session_id: str, tool_call_payload: Any):
        return self.service.handle_tool_call(session_id, tool_call_payload)

    def get_chat_summary(self, session_id: str):
        return self.service.get_chat_summary(session_id)


# Global instance for compatibility with existing imports
chat_manager = ChatManager()


__all__ = ["ChatManager", "chat_manager"]
